-
Notifications
You must be signed in to change notification settings - Fork 0
【Arai60】29問目 105. Construct Binary Tree from Preorder and Inorder Traversal #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
||
current_preorder_index = 0 | ||
|
||
def array_to_tree(inorder_left, inorder_right): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
区間は [inorder_left, inorder_right) の開閉区間で持たせたほうが一般的だと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分の分かりやすい方を主観で決めて実装していました。
開閉区間で統一するようにします。
) -> Optional[TreeNode]: | ||
current_preorder_index = 0 | ||
|
||
def array_to_tree(splied_inorder): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
splied という単語が辞書で見つかりませんでした。綴りをご確認いただいてもよろしいでしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
splitのtypoでした。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
良いと思います。別途preorder_indexを管理しない方法でも書いてみても良いかなと思います。
nonlocal current_preorder_index | ||
if inorder_left >= inorder_right: | ||
return None | ||
val = preorder[current_preorder_index] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
valだけだとこれは何の値を意味するのかがわからないかなと思います。どこまで変数名として表現するかは難しいですが、valだけでは、[inorder_left, inorder_right)の区間で表されるTreeのrootの値ということを読み取るのは難しいと思います。
問題
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/